<?php

namespace App\Http\Controllers;

use Validator;
use App\Http\Controllers\Controller;
use App\Repositories\ImgListRepositoryEloquent;
use Illuminate\Http\Request;
use App\Criteria\BoreListCriteria;
use App\Criteria\AmazeListCriteria;

header("Access-Control-Allow-Origin:*");
/*星号表示所有的域都可以接受，*/
header("Access-Control-Allow-Methods:GET,POST");
class ListController extends Controller
{

    /**
     * @var ArtistRankRepositoryEloquent
     */
    protected $img_repository;
    protected $artist_production_repository;

    public function __construct(ImgListRepositoryEloquent $repository){
        $this->img_repository = $repository;
    }
    /**
     * 无聊图列表
     * @author   ljx
     * @date 2017-09-03
     * @param string $url   请求地址
     * @return null
     *
     **/
    public function boreList(Request $request) {
        $page = $request->input('page',1);
        $validator = Validator::make(['page' => $page], [
            'page' => 'required:numeric'
        ]);
        if($validator->fails()) {
            return response()->json($validator->errors());
        }
        $this->img_repository->pushCriteria(BoreListCriteria::class);
        $data = $this->img_repository->paginate(5);
        if($data->isEmpty()) {
            return response()->json(['code' => 0, 'msg' => '暂无数据']);
        }
        $data = $data->toArray();
        foreach ($data['data'] as &$value) {
            $value['content'] = json_decode($value['content'],true);
        }
        return response()->json(['code' => 1, 'data' => $data]);
    }
    /**
     * 妹子列表
     * @author   ljx
     * @date 2017-09-03
     * @param string $url   请求地址
     * @return null
     *
     **/
    public function amazeList(Request $request) {
        $page = $request->input('page',1);
        $validator = Validator::make(['page' => $page], [
            'page' => 'required:numeric'
        ]);
        if($validator->fails()) {
            return response()->json($validator->errors());
        }
        $this->img_repository->pushCriteria(AmazeListCriteria::class);
        $data = $this->img_repository->paginate(5);
        if($data->isEmpty()) {
            return response()->json(['code' => 0, 'msg' => '暂无数据']);
        }
        $data = $data->toArray();
        foreach ($data['data'] as &$value) {
            $value['content'] = json_decode($value['content'],true);
        }
        return response()->json(['code' => 1, 'data' => $data]);
    }
    /**
     *
     * @author   ljx
     * @date 2017-09-05
     * @param string $url   请求地址
     * @return null
     *
     **/
    public function praise(Request $request) {
        $id = $request->input('id');
        $validator = Validator::make(['id' => $id], [
            'id' => 'required:numeric'
        ]);
        if($validator->fails()) {
            return response()->json($validator->errors());
        }
        $data = $this->img_repository->find($id, ['praise']);
        if(!empty($data)) {
            $this->img_repository->update(['praise' => $data->praise+1], $id);
            return response()->json(['code' => 1, 'msg' => '点赞成功']);
        }else {
            return response()->json(['code' => 0, 'msg' => '暂无数据']);
        }
    }
    /**
     *
     * @author   ljx
     * @date 2017-09-05
     * @param string $url   请求地址
     * @return null
     *
     **/
    public function disdain(Request $request) {
        $id = $request->input('id');
        $validator = Validator::make(['id' => $id], [
            'id' => 'required:numeric'
        ]);
        if($validator->fails()) {
            return response()->json($validator->errors());
        }
        $data = $this->img_repository->find($id, ['disdain']);
        if(!empty($data)) {
            $this->img_repository->update(['disdain' => $data->disdain+1], $id);
            return response()->json(['code' => 1, 'msg' => '鄙视一下']);
        }else {
            return response()->json(['code' => 0, 'msg' => '暂无数据']);
        }
    }
}
